#include	"WindowCode.hpp"

WindowCode::WindowCode(unsigned int heightPercentage, unsigned int widthPercentage)
{
  this->_height = LINES * heightPercentage / 100;
  this->_width = COLS * widthPercentage / 100;
  this->_win = newwin(this->_height, this->_width, 0, 0);
  box(this->_win, 0, 0);
  wrefresh(this->_win);
}

WindowCode::~WindowCode()
{
}

void			WindowCode::setContentWin(const char *name_file)
{
  std::string	a("./");

  a.append(name_file);
  this->_subWinCode->setContentWin(a.c_str());
  a.clear();
  a = "./norme/";
  a.append(name_file);
  a.append(".warn");
  this->_subWinWarning->setContentWin(a.c_str(), this->_subWinCode->getContentTree()->getList().size());
}

void			WindowCode::createSubWindows()
{
  this->_subWinWarning = new SubWinWarning(this->_height, this->_width, this->_win);
  this->_subWinLines = new SubWinLines(this->_height, this->_width, this->_win, this->_subWinWarning->getWidthtWin());
  this->_subWinCode = new SubWinCode(this->_height, this->_width, this->_win, this->_subWinWarning->getWidthtWin(), this->_subWinLines->getWidthtWin());
}



void	WindowCode::checkEvent(int event)
{
  if (event == KEY_DOWN || event == KEY_UP)
    {
      this->_subWinCode->checkEvent(event);
      this->_subWinWarning->checkEvent(event);
    }
  else if (event == KEY_LEFT || event == KEY_RIGHT)
    {
      this->_subWinWarning->checkEvent(event);
    }
}

void	WindowCode::update(bool focus)
{
  if (focus)
    enableColorPair1(this->_win);
  box(this->_win, 0, 0);
  if (focus)
    disableColorPair1(this->_win);
  
  //updateSubWin()
  this->_subWinCode->update(false);
  
  //updateSubWin()
  this->_subWinWarning->update(false);
    
  //update sub win Lines
  this->_subWinLines->update(false);
  this->_subWinLines->updateLines(this->_subWinCode->getScrollText()->getStartDisplay(), this->_subWinCode->getHeightWin() - 2);
  wrefresh(this->_win);
}
